class Ecase < ActiveRecord::Base
  belongs_to :country
  belongs_to :timeunit
  belongs_to :author, :class_name => "User", :foreign_key => "author_id"
  belongs_to :slideshow
  has_many :documents, :dependent => :destroy
  has_many :videos, :dependent => :destroy
  has_many :images, :dependent => :destroy
  has_many :links, :dependent => :destroy
  has_many :slideshow_images, :through => :slideshow
  has_many :events, :dependent => :destroy
  has_many :comments, :as => :commentable ,:dependent => :destroy , :order => "created_at DESC" 
  has_many :last_comments,:class_name => "Comment", :as => :commentable , :order => "created_at DESC" ,:limit =>20
  has_one :ecase_region, :dependent => :destroy
  validates_presence_of :country_id,:title,:timeunit_id
  
  def date_formatted
    att = @attributes['date']
    "#{att[8,2]} #{Date::MONTHNAMES[att[5,2].to_i]} #{att[0,4]}" 
  end

  def date_time
    DateTime.parse(@attributes['date'])
  end

  def get_type
    self.class.to_s.downcase
  end

  def get_controller
    "ecases"
  end

  def about_authors_filled
    (about_authors.nil? || about_authors.size < 1) ? EMPTY_EDITOR_TEXT : about_authors
  end
  
  def description_filled
    (description.nil? || description.size < 1) ? EMPTY_EDITOR_TEXT : description
  end 
 
  def teaching_notes_filled
    (teaching_notes.nil? || teaching_notes.size < 1) ? EMPTY_EDITOR_TEXT : teaching_notes
  end 
  
  def teaching_notes_with_check
    teaching_text = '<span class="teaching_info" > Teaching notes are not available for this case study.</span>'
    (teaching_notes.nil? || teaching_notes.size < 1) ? teaching_text : teaching_notes
  end
end